﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Autoroute.Models;

namespace RaisingStudio.Razor.Utilities
{
    public class PathBuilder
    {
        private readonly IContent _content;
        private string _path;

        public PathBuilder(IContent content)
        {
            _content = content;
            _path = "";
        }

        public override string ToString()
        {
            return _path;
        }

        public PathBuilder AddContentType()
        {
            Add(_content.ContentItem.ContentType);
            return this;
        }

        public PathBuilder AddContainerSlug()
        {
            var common = _content.As<ICommonPart>();
            if (common == null)
                return this;

            var routable = common.Container.As<AutoroutePart>();
            if (routable == null)
                return this;

            Add(routable.Path);
            return this;
        }

        public PathBuilder AddSlug()
        {
            var routable = _content.As<AutoroutePart>();
            if (routable == null)
                return this;

            Add(routable.Path);
            return this;
        }

        private void Add(string segment)
        {
            if (string.IsNullOrEmpty(segment))
                return;
            if (string.IsNullOrEmpty(_path))
                _path = segment;
            else
                _path = _path + "/" + segment;
        }
    }
}